package cn.zyl.demo.java.panel;

import cn.zyl.demo.common.enums.FontStyleEnum;
import cn.zyl.demo.common.tools.FontTool;

import javax.swing.*;
import java.awt.*;

/**
 * DrawSimple 类说明: 简单绘图demo
 *
 * @author wsz
 * @version v1.0
 * @date 2020-08-18
 */
public class DrawSimple extends JPanel{
    @Override
    public void paintComponent(Graphics gp) {
        super.paintComponent(gp);
        Graphics2D gp2d = (Graphics2D) gp;
        drawLattice(gp2d);
        drawLine(gp2d);
        drawRect(gp2d);
        drawOval(gp2d);
        drawArc(gp2d);
        drawOther(gp2d);
        drawText(gp2d);
    }
    /** 画网格*/
    private void drawLattice(Graphics2D gp2d){
        int width = this.getWidth();
        int height = this.getHeight();
        //格子间隔
        int interval = 20,tmp;
        Color color = new Color(141, 141, 141, 128);
        gp2d.setColor(color);
        //画竖线
        for (int i = 1,size = width / interval; i < size; i++) {
            tmp = i * interval;
            gp2d.drawLine(tmp, 0, tmp, height-10);
            if(i%2 ==0){gp2d.drawString(tmp+"", tmp-10, height);}
        }
        //画横线
        for (int i = 1,size = height / interval; i < size; i++) {
            tmp = i * interval;
            gp2d.drawLine(0, tmp, width-20, tmp);
            if(i%2 ==0){gp2d.drawString(tmp+"", width-20, tmp+5);}
        }
    }
    /** 画文字*/
    private void drawText(Graphics2D gp2d){
        Font font = FontTool.randomFont(FontStyleEnum.BOLD,40,false);
        //字体样式 字体格式 字体大小
        gp2d.setFont(font);
        //设置字体颜色
        gp2d.setColor(Color.GREEN);
        //写字符串
        gp2d.drawString("hello word."+font.getName(), 40, 400);
        System.out.println(font.getName());
    }
    /** 画椭圆，矩形，线*/
    private void drawOther(Graphics2D gp2d){
        //笔画属性
        BasicStroke bs = new BasicStroke( 10.1f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL);
        //创建红绿蓝不透明的srg b颜色
        Color color = new Color(22, 147, 140);
        gp2d.setColor(color);
        //绘制椭圆的边框。
        gp2d.drawOval(10, 200, 100, 60);
        gp2d.fill3DRect(150, 30, 100, 80, false);
        gp2d.fillOval(150,130, 100, 80);
        //设置笔画属性
        gp2d.setStroke(bs);
        gp2d.drawLine(300, 80, 300, 200);
    }
    /** 画扇形*/
    private void drawArc(Graphics2D gp2d){
        gp2d.setColor(Color.gray);
        gp2d.fillArc(35, 160, 40, 25, 100, 270);
    }
    /** 画2个椭圆*/
    private void drawOval(Graphics2D gp2d){
        gp2d.setColor(Color.black);
        gp2d.fillOval(20, 130, 30, 20);
        gp2d.fillOval(70, 130, 30, 20);
    }
    /** 画2个矩形*/
    private void drawRect(Graphics2D gp2d){
        gp2d.setColor(Color.blue);
        //绘制矩形的x坐标，绘制矩形的y坐标，矩形的宽度，矩形的高度
        gp2d.drawRect(10,50,100,50);
        //绘制矩形的x坐标，绘制矩形的y坐标，矩形的宽度，矩形的高度,4个角弧度的水平直径，4个角弧度的垂直直径
        gp2d.drawRoundRect(10, 120, 100, 70, 12, 12);
    }
    /** 画线*/
    private void drawLine(Graphics2D gp2d){
        gp2d.setColor(Color.blue);
        //第一个点的x坐标，第一个点的y坐标，第二个点的x坐标，第二点的坐标
        gp2d.drawLine(30, 30, 80, 30);
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("简单绘图demo");
        //内容
        frame.getContentPane().add(new DrawSimple());
        //宽高
        frame.setSize(800, 600);
        //窗体居中
        frame.setLocationRelativeTo(null);
        //设置窗体可见 verification code
        frame.setVisible(true);
        //设置关闭窗体就是关闭整个程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
